package com.mg.admin.aop;

import com.alibaba.fastjson.JSON;
import com.mg.admin.bean.admin.LogVO;
import com.mg.admin.bean.admin.Result;
import com.mg.admin.bean.admin.UserVO;
import com.mg.admin.constants.Constants;
import com.mg.admin.service.admin.LogService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.Date;

/**
 * Created by Administrator on 2017-09-21.
 */
@Aspect
@Component
public class LogAop {

    @Autowired
    private LogService logService;

    private String saveTpl = "用户名：{0},修改数据，提交参数：{1}";

    private String userLoginTpl = "用户：{0}登录管理后台,{1}";


    @Pointcut("execution(* com.mg.admin.controller.admin.*.*save(..))")
    public void savepoint() {
    }

    @Pointcut("execution(* com.mg.admin.controller.admin.IndexController.loginSubmit(..))")
    public void loginpoint() {
    }


    @SuppressWarnings("unchecked")
    @AfterReturning(pointcut = "savepoint()", returning = "result")
    public void afterSaveReturn(JoinPoint joinPoint, Object result) {

        Subject subject = SecurityUtils.getSubject();

        Session session = subject.getSession();

        UserVO userVO = (UserVO) session.getAttribute(Constants.SESSION_KEY);

        LogVO logVO = new LogVO();

        logVO.setUsername(userVO.getUsername());
        logVO.setRemark(JSON.toJSONString(joinPoint.getArgs()));
        logVO.setLoginIp(session.getHost());
        logVO.setGmtCreate(new Date());

        String editAccountStr = MessageFormat.format(saveTpl, logVO.getUsername(), logVO.getRemark());

        logVO.setRemark(editAccountStr);
        logService.insert(logVO);
    }


    @SuppressWarnings("unchecked")
    @AfterReturning(pointcut = "loginpoint()", returning = "result")
    public void afterLoginReturn(JoinPoint joinPoint, Object result) {

        Subject subject = SecurityUtils.getSubject();

        Session session = subject.getSession();

        Result<String> stringResult = (Result<String>) result;

        Object[] methodArgs = joinPoint.getArgs();

        UserVO userVO = (UserVO) methodArgs[0];

        LogVO logVO = new LogVO();
        logVO.setUsername(userVO.getUsername());
        logVO.setRemark(JSON.toJSONString(methodArgs));
        logVO.setLoginIp(session.getHost());
        logVO.setGmtCreate(new Date());

        String loginStatus = "";

        if (stringResult.isSuccess()){
            loginStatus = "成功";
        }else {
            loginStatus = "失败，提交参数："+ logVO.getRemark();
        }
        String str = MessageFormat.format(userLoginTpl,logVO.getUsername(),loginStatus);
        logVO.setRemark(str);

        logService.insert(logVO);

    }



}
